﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Txb_Email.Attributes["onblur"] = "MyJsFunc()";

        if (!IsPostBack)
        {
            Panel2.Visible = false;
            Panel3.Visible = false;
            init_org();
        }
        
    }


    [System.Web.Services.WebMethod]
    public static string MyMethod(string userName)
    {
        bool result = false;

        UserMemberService.UserMemberService oService = new UserMemberService.UserMemberService();
        result =  oService.IsLoginEmailAvaible(userName);

        if ( result==false)
            return "false";
        else
            return "true";
    }


    protected void Btn_FirNext_Click(object sender, EventArgs e)
    {
        if (Lbl_Show.Text.ToString() != "")
        {
            this.RegisterStartupScript("Msg", "<script>alert('该邮箱已经注册！')</script>");
            return;
        }

        if (Txb_Email.Text.ToString() == "")
        {
            this.RegisterStartupScript("Msg", "<script>alert('请输入登录Email')</script>");
            return;
        }
        if(Txb_Pwd.Text.ToString() == "")
        {
            this.RegisterStartupScript("Msg", "<script>alert('请输入密码')</script>");
            return;
        }

        if (Rbtn_Persion.Checked == true)
        {
            div_Org.Visible = false;
            div_Type.Visible = false;
            div_ShopAttr.Visible = false;
        }
        else if (Rbtn_Ent.Checked == true) 
        {
            div_Org.Visible = false;
            div_ShopAttr.Visible = false;
        }
        else if (Rbtn_Shop.Checked == true)
        {
            div_Org.Visible = false;
           
        }
        else if (Rbtn_Org.Checked == true) 
        {
            div_Type.Visible = false;
            div_ShopAttr.Visible = false;
        }

        Panel1.Visible = false;
        Panel2.Visible = true;
    }


    protected void Btn_SecNext_Click(object sender, EventArgs e)
    {
        

        string strNo = "";

        if ( Rbtn_Persion.Checked == true)
        {
            if (DDL_Distict.SelectedItem.Text.ToString() == "请选择")
            {
                this.RegisterStartupScript("Msg", "<script>alert('请选择所在地区')</script>");
                return;
            }
            else
            {
                
                ToolService.ToolService oService = new ToolService.ToolService();

                strNo = oService.GenerateDBLNo(DDL_Distict.SelectedValue.ToString(),"","","P","");

                Lbl_No.Text = strNo;

            }

            div_PersionInfo.Visible = true;
        }

        else if (Rbtn_Ent.Checked == true) 
        {
            if ((DDL_Distict.SelectedItem.Text.ToString() == "请选择") || (DDL_SType.SelectedItem.Text.ToString() == "请选择"))
            {
                this.RegisterStartupScript("Msg", "<script>alert('请选择所在地区和所属行业')</script>");
                return;
            }
            else
            {

                ToolService.ToolService oService = new ToolService.ToolService();

                strNo = oService.GenerateDBLNo(DDL_Distict.SelectedValue.ToString(),DDL_SType.SelectedValue.ToString(), "", "C", "");

                Lbl_No.Text = strNo;
            }
        }


        else if (Rbtn_Org.Checked == true) 
        {
            if ((DDL_Distict.SelectedItem.Text.ToString() == "请选择") || (DDL_Org.SelectedItem.Text.ToString() == "请选择"))
            {
                this.RegisterStartupScript("Msg", "<script>alert('请选择所在地区和组织类别')</script>");
                return;
            }
            else
            {

                ToolService.ToolService oService = new ToolService.ToolService();

                strNo = oService.GenerateDBLNo(DDL_Distict.SelectedValue.ToString(), "", "", "O", DDL_Org.SelectedValue.ToString() );

                Lbl_No.Text = strNo;
                
            }
        }

        else if (Rbtn_Shop.Checked == true) 
        {
            if ((DDL_Distict.SelectedItem.Text.ToString() == "请选择") || (DDL_SType.SelectedItem.Text.ToString() == "请选择")
                || (DDL_ShopAttr.SelectedItem.Text.ToString() == "请选择"))
            {
                this.RegisterStartupScript("Msg", "<script>alert('请选择所在地区、所属行业和商铺规模')</script>");
                return;
            }
            else
            {

                ToolService.ToolService oService = new ToolService.ToolService();

                strNo = oService.GenerateDBLNo(DDL_Distict.SelectedValue.ToString(), DDL_SType.SelectedValue.ToString(), DDL_ShopAttr.SelectedValue.ToString(), "S", "");

                Lbl_No.Text = strNo;
               

            }

            div_ShopInfo.Visible = true;
        }

        Panel2.Visible = false;
        Panel3.Visible = true;

    }


    protected void Btn_Finish_Click(object sender, EventArgs e)
    {
        string strNo = "";
        int ret=0;

        if (Lbl_No.Text.ToString() == "")
        {
            this.RegisterStartupScript("Msg", "<script>alert('用户注册异常，请重新注册！')</script>");
            return;
        }

        UserMemberService.UserMemberService oService = new UserMemberService.UserMemberService();
        
        DataSet ds = new DataSet();
        int id = 0;

        if (Rbtn_Persion.Checked == true)
        {
            ret=oService.AddUserMember(Lbl_No.Text.ToString(), Txb_Email.Text.ToString(), Txb_Pwd.Text.ToString(), 0, "P", "", "");

            if (ret <= 0)
            {
                this.RegisterStartupScript("Msg", "<script>alert('用户注册异常，请重新注册！')</script>");
                return;
            }

            ds = oService.GetUserMemberByDBLNo(Lbl_No.Text.ToString());
            id = Convert.ToInt32( ds.Tables[0].Rows[0].ItemArray[0].ToString());

            if (id > 0)
            {
                UserPersonService.UserPersonService oService2 = new UserPersonService.UserPersonService();
          
                ret = oService2.AddUpdateUserPerson(id, Lbl_No.Text.ToString(), Txb_NickName.Text.ToString(),
                        Txb_FullName.Text.ToString(), Txb_KeyWord1.Text.ToString(), Txb_KeyWord2.Text.ToString(),
                        Txb_KeyWord3.Text.ToString(), Txb_PID.Text.ToString(),
                        Txb_Address.Text.ToString(), Txb_Demo.Text.ToString(), Txb_Phone.Text.ToString(), Txb_FAX.Text.ToString(),
                        Txb_Email.Text.ToString(), Txb_Mobile.Text.ToString(), Txb_QQ.Text.ToString(), Txb_MSN.Text.ToString(),
                        Txb_Blog.Text.ToString(), Txb_Weibo.Text.ToString(), Txb_WebSite.Text.ToString());

                if (ret <= 0)
                {
                    ret = oService.DeleteUserMemberByID(id);

                    this.RegisterStartupScript("Msg", "<script>alert('用户注册异常，请重新注册！')</script>");
                    return;
                }
            }
            

        }

        else if (Rbtn_Ent.Checked == true)
        {
            ret = oService.AddUserMember(Lbl_No.Text.ToString(), Txb_Email.Text.ToString(), Txb_Pwd.Text.ToString(), 0, "C", "", "");

            if (ret <= 0)
            {
                this.RegisterStartupScript("Msg", "<script>alert('用户注册异常，请重新注册！')</script>");
                return;
            }

            ds = oService.GetUserMemberByDBLNo(Lbl_No.Text.ToString());
            id = Convert.ToInt32(ds.Tables[0].Rows[0].ItemArray[0].ToString());

            if (id > 0)
            {
                UserCompanyService.UserCompanyService oService2 = new UserCompanyService.UserCompanyService();

                ret = oService2.AddUpdateUserCompany(id, Lbl_No.Text.ToString(), Txb_NickName.Text.ToString(),
                        Txb_FullName.Text.ToString(), Txb_KeyWord1.Text.ToString(), Txb_KeyWord2.Text.ToString(), Txb_KeyWord3.Text.ToString(),
                Txb_Address.Text.ToString(), Txb_Demo.Text.ToString(), Txb_Phone.Text.ToString(), Txb_FAX.Text.ToString(),
                    Txb_Email.Text.ToString(), Txb_Mobile.Text.ToString(),  Txb_QQ.Text.ToString(), Txb_MSN.Text.ToString(),
                        Txb_Blog.Text.ToString(), Txb_Weibo.Text.ToString(), Txb_WebSite.Text.ToString());

                if (ret <= 0)
                {
                    ret = oService.DeleteUserMemberByID(id);

                    this.RegisterStartupScript("Msg", "<script>alert('用户注册异常，请重新注册！')</script>");
                    return;
                }
            }
           
        }


        else if (Rbtn_Org.Checked == true)
        {
            ret = oService.AddUserMember(Lbl_No.Text.ToString(), Txb_Email.Text.ToString(), Txb_Pwd.Text.ToString(), 0, "O", "", "");

            if (ret <= 0)
            {
                this.RegisterStartupScript("Msg", "<script>alert('用户注册异常，请重新注册！')</script>");
                return;
            }

            ds = oService.GetUserMemberByDBLNo(Lbl_No.Text.ToString());
            id = Convert.ToInt32(ds.Tables[0].Rows[0].ItemArray[0].ToString());

            if (id > 0)
            {
                UserOrganizationService.UserOrganizationService oService2 = new UserOrganizationService.UserOrganizationService();

                //ret = oService2.AddUpdateUserOrganization(id, Lbl_No.Text.ToString(), Txb_NickName.Text.ToString(),
                //        Txb_FullName.Text.ToString(), Txb_KeyWord1.Text.ToString(), Txb_KeyWord2.Text.ToString(),
                //        Txb_KeyWord3.Text.ToString(), "",
                //        Txb_Address.Text.ToString(), "", Txb_Phone.Text.ToString(), "",
                //        Txb_Email2.Text.ToString(), Txb_Mobile.Text.ToString(), "", "",
                //        "", "", "");


                ret = oService2.AddUpdateUserOrganization(id, Lbl_No.Text.ToString(), Txb_NickName.Text.ToString(),
                        Txb_FullName.Text.ToString(), Txb_KeyWord1.Text.ToString(), Txb_KeyWord2.Text.ToString(), Txb_KeyWord3.Text.ToString(),
                        Txb_Address.Text.ToString(), Txb_Demo.Text.ToString(), Txb_Phone.Text.ToString(), Txb_FAX.Text.ToString(),
                        Txb_Email.Text.ToString(), Txb_Mobile.Text.ToString(), Txb_QQ.Text.ToString(), Txb_MSN.Text.ToString(),
                        Txb_Blog.Text.ToString(), Txb_Weibo.Text.ToString(), Txb_WebSite.Text.ToString());

                if (ret <= 0)
                {
                    ret = oService.DeleteUserMemberByID(id);

                    this.RegisterStartupScript("Msg", "<script>alert('用户注册异常，请重新注册！')</script>");
                    return;
                }
            }
        }

        else if (Rbtn_Shop.Checked == true)
        {
            ret = oService.AddUserMember(Lbl_No.Text.ToString(), Txb_Email.Text.ToString(), Txb_Pwd.Text.ToString(), 0, "O", "", "");

            if (ret <= 0)
            {
                this.RegisterStartupScript("Msg", "<script>alert('用户注册异常，请重新注册！')</script>");
                return;
            }

            ds = oService.GetUserMemberByDBLNo(Lbl_No.Text.ToString());
            id = Convert.ToInt32(ds.Tables[0].Rows[0].ItemArray[0].ToString());

            if (id > 0)
            {
                UserShopService.UserShopService oService2 = new UserShopService.UserShopService();

                ret = oService2.AddUpdateUserShop(id, Lbl_No.Text.ToString(), Txb_NickName.Text.ToString(),
                        Txb_FullName.Text.ToString(), Txb_KeyWord1.Text.ToString(), Txb_KeyWord2.Text.ToString(),
                        Txb_KeyWord3.Text.ToString(),
                        Txb_Address.Text.ToString(),  Txb_Demo.Text.ToString(), Txb_Phone.Text.ToString(),  Txb_FAX.Text.ToString(),
                        Txb_Email.Text.ToString(), Txb_Mobile.Text.ToString(), Txb_ShopBoss.Text.ToString() , Txb_QQ.Text.ToString(), Txb_MSN.Text.ToString(),
                        Txb_Blog.Text.ToString(), Txb_Weibo.Text.ToString(), Txb_WebSite.Text.ToString());

                if (ret <= 0)
                {
                    ret = oService.DeleteUserMemberByID(id);

                    this.RegisterStartupScript("Msg", "<script>alert('用户注册异常，请重新注册！')</script>");
                    return;
                }
            }
        }


        this.RegisterStartupScript("Msg", "<script>alert('注册成功！')</script>");

        Response.Redirect("Search.aspx");
    }


    public void init_org()
    {
        DataSet ds = new DataSet();
        ToolService.ToolService oService = new ToolService.ToolService();

        ds = oService.GetAllProvince();
        DDL_Province.DataSource = ds;
        DDL_Province.DataTextField = "PROVINCENAME";
        DDL_Province.DataValueField = "CODE";
        DDL_Province.DataBind();


        ds = oService.GetAllCategory();
        DDL_Type.DataSource = ds;
        DDL_Type.DataTextField = "CATEGORYNAME";
        DDL_Type.DataValueField = "CATEGORYCODE";
        DDL_Type.DataBind();


        ds = oService.GetAllOrganizationType();
        DDL_Org.DataSource = ds;
        DDL_Org.DataTextField = "ORGNAME";
        DDL_Org.DataValueField = "ORGCODE";
        DDL_Org.DataBind();

        ds = oService.GetAllShopAttribute();
        DDL_ShopAttr.DataSource = ds;
        DDL_ShopAttr.DataTextField = "NAME";
        DDL_ShopAttr.DataValueField = "CODE";
        DDL_ShopAttr.DataBind();

    }
    protected void DDL_Province_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DDL_Province.SelectedItem.Text.ToString() != "请选择")
        {
            DataSet ds = new DataSet();
            ToolService.ToolService oService = new ToolService.ToolService();

            ds = oService.GetCityByProvinceCode(DDL_Province.SelectedValue.ToString());

            DDL_City.DataSource = ds;

            DDL_City.DataTextField = "CITYNAME";

            DDL_City.DataValueField = "CITYCODE";

            DDL_City.DataBind();
        }

    }
    protected void DDL_City_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DDL_City.SelectedItem.Text.ToString() != "请选择")
        {
            DataSet ds = new DataSet();
            ToolService.ToolService oService = new ToolService.ToolService();

            ds = oService.GetDistrictByCityCode(DDL_City.SelectedValue.ToString());

            DDL_Distict.DataSource = ds;

            DDL_Distict.DataTextField = "DISTRICTNAME";

            DDL_Distict.DataValueField = "DISTRICTCODE";

            DDL_Distict.DataBind();
        }
    }
    protected void DDL_Type_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DDL_Type.SelectedItem.Text.ToString() != "请选择")
        {
            DataSet ds = new DataSet();
            ToolService.ToolService oService = new ToolService.ToolService();

            ds = oService.GetClassificationByCategory(DDL_Type.SelectedValue.ToString());

            DDL_SType.DataSource = ds;

            DDL_SType.DataTextField = "CLASSIFICATIONNAME";

            DDL_SType.DataValueField = "CLASSIFICATIONFULLCODE";

            DDL_SType.DataBind();
        }
    }
}
